#!/bin/bash

##########################################################
#                 构建docker镜像                          #
##########################################################

echo "构建镜像开始执行......."

#时间格式化
TIME_TAG=`date "+%Y%m%d%H%M"`
#dokcer 镜像地址
IMAGE_REPOSITORY=hub.zhang.com/koala/
#git 提交版本
GIT_VERSION=`git log -1 --pretty=format:"%h"`
#应用名称
MOUDLE=(koala-auth koala-config koala-euraka koala_gateway)

for moudleName in ${MOUDLE[@]};do
echo "构建$moudleName is begining......"

#组装docker镜像的名称
IMAGE_NAME=${IMAGE_REPOSITORY}$moudleName:${TIME_TAG}_${GIT_VERSION}

echo "开始构建镜像：${IMAGE_NAME}"
docker build -f $moudleName/Dockerfile -t  ${IMAGE_NAME} .

echo "开始推送镜像:${IMAGE_NAME}至私有仓库：${IMAGE_REPOSITORY}"
docker push ${IMAGE_NAME}

echo "删除当前镜像：${IMAGE_NAME}"
docker rmi ${IMAGE_NAME}

#将构建好的镜像名称存到本地，后面发布的时候会用到
echo "${IMAGE_NAME}" > $1/image_$moudleName
done

docker build -f koala-service-api/koala-upms/Dockerfile -t ${IMAGE_REPOSITORY}koala-upms:${TIME_TAG}_${GIT_VERSION} .
docker push ${IMAGE_REPOSITORY}koala-upms:${TIME_TAG}_${GIT_VERSION}
docker rmi ${IMAGE_REPOSITORY}koala-upms:${TIME_TAG}_${GIT_VERSION}
#将构建好的镜像名称存到本地，后面发布的时候会用到
echo "${IMAGE_REPOSITORY}koala-upms:${TIME_TAG}_${GIT_VERSION}" > $1/image_koala-upms
echo "构建koala-upms is ending..."
echo "所有镜像构建、推送完毕..."
